#include "I2cCommunicationModule.h"
#include "BaseDef.h"

#include <string.h>

#include <ulog.h>

// rtc地址
uint8_t rtc_addr = 0x32;

// 全局时间结构体
Time_Info_Def sys_time_info;

// rtc
Rtc_Ctrl_Task_Def rtc_ctrl_task;

extern rt_thread_t rtc_check_tid;
extern rt_thread_t rtc_flash_tid;
extern rt_thread_t debug_usart_tid;

// 时间管理线程主函数
// 主要负责外部事件同步到RTC，以及外部RTC更新内部RTC
void Time_Manager_Thread(void *parmameter) {
  // 先等候系统初始化完成
  for (;;) {
    if (debug_usart_tid != RT_NULL && STATU_GET(sys_status, SYS_LOG_OK)) {
      break;
    } else {
      rt_thread_mdelay(1);
    }
  }

  // 设置内部RTC对象启动
  //STATU_SET(rtc_ctrl_task.rtc_status, RTC_INITED);
  for (;;) {                                                  // 时间管理任务主循环
    if (STATU_GET_NOT(rtc_ctrl_task.rtc_status, RTC_INITED)) {// 初始化RTC环境
      // 如果存在外部RTC
      if (STATU_GET(rtc_ctrl_task.rtc_status, HAD_EXT_RTC)) {
        // 检查芯片是否已经完成初始化, 目前方式是读芯片时间

        if (Ext_Rtc_Stoped(&rtc_ctrl_task) == RT_EBUSY) {// 出错了
          for (;;) { ; }
        }// 出错了

        // 如果芯片重启过就重新初始化
        if (STATU_GET(rtc_ctrl_task.ext_rtc_status, EXT_RTC_STOPED)) {
          ULOG_WARNING("检测到RTC芯片发生过重启\n");
          STATU_SET(rtc_ctrl_task.ext_rtc_status, EXT_RTC_32K_NVIC);
          STATU_RESET(rtc_ctrl_task.ext_rtc_status, EXT_RTC_INIT_PRE_TIMER_NVIC);

          // 清空校准寄存器
          Ext_Rtc_Reset_Tmp_Ttf(&rtc_ctrl_task);

          // 如果芯片重启过，就更新时间到外部RTC
          //Sd8804_Write_Data_Time(&rtc_ctrl_task, &sys_time_info);
        }// 如果芯片重启过就重新初始化, 如果初始化完成了就不执行了

        // 这颗芯片的RTC的32.768K输出是默认启动的
        if (rtc_addr == 0x32) {
          STATU_SET(rtc_ctrl_task.rtc_status, EXT_RTC_32K_NVIC);
        }

        // 打开秒中断
        Sd8804_Init_Sec_Intc(&rtc_ctrl_task, true);

      }// 如果存在外部RTC

      // 如果存在内部RTC
      // 这里暂时没啥事要做

      // 初始化完成
      STATU_SET(rtc_ctrl_task.rtc_status, RTC_INITED);
    }// 初始化RTC环境

    // 时间管理任务

    // 先检查是否明确需要对时，否则就等候秒中断更新时钟
    // 这里不再周期检查任务。因为没必要
    if (STATU_GET(rtc_ctrl_task.ext_rtc_flash_squence, EXT_RTC_CHECK_TIME_OK)) {
      rt_uint32_t e;
      rt_event_recv(&(rtc_ctrl_task.rtc_ctrl_event), (EVENT_EXT_RTC_START_CHECK_TIME_TASK), (RT_EVENT_FLAG_OR), RT_WAITING_FOREVER, &e);
      //ULOG_DEBUG("get ext rtc check time event: 0x%02X\n", e);
      STATU_RESET(rtc_ctrl_task.ext_rtc_flash_squence, EXT_RTC_CHECK_TIME_OK);
    }// 先检查是否明确需要对时，否则就等候秒中断更新时钟

    if (STATU_GET_NOT(rtc_ctrl_task.ext_rtc_flash_squence, EXT_RTC_CHECK_TIME_OK)) {// 需要更新时间
      // 等候更新完成
      //rt_uint32_t e = RT_EOK;
      // 这里仅仅等候1秒是为了不干涉后面的更新任务
      //rt_event_recv(&(rtc_ctrl_task.rtc_ctrl_event), (EVENT_EXT_RTC_START_CHECK_TIME_TASK_FINISHED), (RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR), 1000, &e);
      //ULOG_DEBUG("get ext rtc check time finished event: 0x%02X\n", e);

      // 更新完成后需要将时间同步到系统。这里不放在对时循环是因为这里没有采用等待式对时。所以需要循环等候更新
      if (sys_time_info.tmp_time_info.update_finished) {
        rt_uint32_t e;
        sys_time_info.Flash_Tmp_To_Time(&sys_time_info);
        // 将时间写入外部RTC
        Sd8804_Write_Enable(&rtc_ctrl_task, false);
        Sd8804_Write_Data_Time(&rtc_ctrl_task, &sys_time_info);
        Sd8804_Write_Disable(&rtc_ctrl_task, false);
        // 更新时间到内部RTC
        sys_time_info.Ext_Rtc_Flash_Inner_Rtc_And_Tm_Time_Info(&sys_time_info);
        sys_time_info.Build_Rtc_Time_Str(&sys_time_info);
        STATU_SET(rtc_ctrl_task.ext_rtc_flash_squence, EXT_RTC_CHECK_TIME_OK);
				sys_time_info.tmp_time_info.update_finished = false;
      } else {
        rt_thread_mdelay(5);
      }// 结束时序同步检查
    }


    // 启动时间字符串更新任务
    if (rtc_flash_tid != RT_NULL &&
        (strlen(sys_time_info.rtc_lock.parent.parent.name) > 0 && strlen(rtc_ctrl_task.i2c_ctrl_obj.name) > 0) &&
        STATU_GET_NOT(rtc_ctrl_task.rtc_status, START_RTC_FLASH_TASK)) {
      //Sd8804_Read_Data_Time(&rtc_ctrl_task, &sys_time_info);
      sys_time_info.Ext_Rtc_Flash_Inner_Rtc_And_Tm_Time_Info(&sys_time_info);// 从外部恢复一次内部RTC时间
      sys_time_info.Build_Rtc_Time_Str(&sys_time_info);
      rt_thread_startup(rtc_flash_tid);
      STATU_SET(rtc_ctrl_task.rtc_status, START_RTC_FLASH_TASK);
    }

    rt_thread_yield();
  }// 时间管理任务主循环
}// 时间管理线程主函数

// 时间更新线程主函数
// 主要负责向外部RTC更新万年历，以及刷新时间字符串
void Flash_Time_Info_Thread(void *parmameter) {
  rt_uint32_t e = RT_EOK;
  char time_str_buf[8] = {0};
  for (;;) {// 任务主循环
    // 等候秒中断,如果需要更新就立刻执行一次更新任务
    //    if (STATU_GET(rtc_ctrl_task.ext_rtc_flash_squence, EXT_RTC_CHECK_TIME_OK)) {
    //
    //    } else {
    //      e = RT_EOK;
    //    }
    rt_event_recv(&(rtc_ctrl_task.rtc_ctrl_event), (EVENT_EXT_RTC_GET_SEC_INTC), (RT_EVENT_FLAG_OR), RT_WAITING_FOREVER, &e);

    // 如果正常运行
    // 由于都是两位数，因此不用特意擦除内存。如果更新年，则单独擦除一次。
    sys_time_info.time_info.tm_sec += 1;
    // 开始检查时间更新
    if (sys_time_info.time_info.tm_sec == 60) {// 如果秒开始进位
      sys_time_info.time_info.tm_sec = 0;
      // 先更新秒数据
      itoa(0, time_str_buf, 2);
      memcpy(sys_time_info.iso_time_str + 17, time_str_buf, 2);

      // 给分钟进位
      sys_time_info.time_info.tm_min += 1;
      if (sys_time_info.time_info.tm_min == 60) {// 分钟发生进位
        sys_time_info.time_info.tm_min = 0;
        // 先更新分钟数据
        itoa(0, time_str_buf, 2);
        memcpy(sys_time_info.iso_time_str + 14, time_str_buf, 2);

        sys_time_info.time_info.tm_hour += 1;
        if (sys_time_info.time_info.tm_hour == 24) {// 小时发生进位
          sys_time_info.time_info.tm_hour = 0;

          // 需要更新日期
          // 需要先初始化才能读外部RTC
          if (STATU_GET(rtc_ctrl_task.rtc_status, RTC_INITED)) {
            Sd8804_Read_Data_Time(&rtc_ctrl_task, &sys_time_info);
          }

          itoa(sys_time_info.time_info.tm_year, time_str_buf, 4);
          memcpy(sys_time_info.iso_time_str, time_str_buf, 4);
          // 年4位数比别的长，所以清空一下
          memset(time_str_buf, 0, 4);
          itoa(sys_time_info.time_info.tm_mon, time_str_buf, 2);
          memcpy(sys_time_info.iso_time_str + 5, time_str_buf, 2);
          itoa(sys_time_info.time_info.tm_mday, time_str_buf, 2);
          memcpy(sys_time_info.iso_time_str + 8, time_str_buf, 2);
        } else {// 如果没发生进位
          // 先更新小时数据
          itoa(0, time_str_buf, 2);
          memcpy(sys_time_info.iso_time_str + 11, time_str_buf, 2);
        }     // 小时发生进位
      } else {// 分钟未发生进位
        itoa(sys_time_info.time_info.tm_min, time_str_buf, 2);
        memcpy(sys_time_info.iso_time_str + 14, time_str_buf, 2);
      }     // 分钟发生进位
    } else {// 如果秒没有进位
      // 仅更新秒数据
      itoa(sys_time_info.time_info.tm_sec, time_str_buf, 2);
      memcpy(sys_time_info.iso_time_str + 17, time_str_buf, 2);
    }// 如果秒开始进位
    //ULOG_DEBUG("get ms: %d\n", sys_time_info.millisecond);
    e = RT_EOK;
  }// 任务主循环
}// 时间更新线程主函数

void INT_SRC_RTC_I2C_RXI_IrqCallback(void) {
  INT_SRC_I2Cx_RXI_IrqCallback(rtc_ctrl_task.i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task.i2c_ctrl_obj));
}

void INT_SRC_RTC_I2C_TXI_IrqCallback(void) {
  INT_SRC_I2Cx_TXI_IrqCallback(rtc_ctrl_task.i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task.i2c_ctrl_obj));
}

void INT_SRC_RTC_I2C_TEI_IrqCallback(void) {
  INT_SRC_I2Cx_TEI_IrqCallback(rtc_ctrl_task.i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task.i2c_ctrl_obj));
}

void INT_SRC_RTC_I2C_EEI_IrqCallback(void) {
  INT_SRC_I2Cx_EEI_IrqCallback(rtc_ctrl_task.i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task.i2c_ctrl_obj));
}

// 打开写保护
rt_err_t Sd8804_Write_Disable(Rtc_Ctrl_Task_Def *rtc_ctrl_task_obj, bool force_check) {
  if (STATU_GET(rtc_ctrl_task_obj->ext_rtc_status, EXT_RTC_WRITABLE) && !force_check) {// 检查写保护状态
    return RT_EOK;
  } else {// 如果确定没有打开写保护
    // 锁定资源。
    if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
      uint8_t get_mutex = 0;
      while (rt_mutex_take(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock), 50 * (2 ^ get_mutex)) != RT_EOK) {
        if (get_mutex >= 3) {
          return LL_ERR_BUSY;
        }
        get_mutex++;
      }
    }// 只有初始化了保护锁才会检查

    // 读写保护状态
    memset(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf, 0, sizeof(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf));
    rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x0F;
    rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 1;
    rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 1;
    I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
    ULOG_DEBUG("get sd8804 reg: 0x%02X value: 0x%02X\n", rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1], rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2]);
    rt_thread_mdelay(2);
    if (STATU_GET(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2], 0x80)) {// 判断写保护已打开
      // 第一步向FC寄存器写0.该寄存器最高位为只读的写保护位，最低两位永远为0，所以写入时候不用管它。
      rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0xFC;
      rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2] = 0x00;
      rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 2;
      rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 0;
      I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
      rt_thread_mdelay(2);
      // 第二步向FC寄存器的2-6位写入11100
      rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2] = 0x70;
      rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 2;
      rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 0;
      I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
      rt_thread_mdelay(2);
      // 第三步向FC寄存器的2-6位写入00011
      rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2] = 0x0C;
      rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 2;
      rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 0;
      I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
      rt_thread_mdelay(2);
      // 第四步向FC寄存器的2-6位写入01110
      rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2] = 0x38;
      rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 2;
      rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 0;
      I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
      rt_thread_mdelay(2);
    }// 判断写保护已打开

    // 读写保护状态
    memset(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf, 0, 3);
    rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x0F;
    rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 1;
    rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 1;
    I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
    ULOG_DEBUG("get sd8804 reg: 0x%02X value: 0x%02X\n", rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1], rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2]);
    rt_thread_mdelay(2);

    if (STATU_GET_NOT(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2], 0x80)) {// 打开成功
      STATU_SET(rtc_ctrl_task_obj->ext_rtc_status, EXT_RTC_WRITABLE);
    } else {// 打开失败
      STATU_RESET(rtc_ctrl_task_obj->ext_rtc_status, EXT_RTC_WRITABLE);
    }

    if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {
      rt_mutex_release(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock));
    }
    return RT_EOK;
  }// 如果确定没有打开写保护
}

// 关闭写保护
rt_err_t Sd8804_Write_Enable(Rtc_Ctrl_Task_Def *rtc_ctrl_task_obj, bool force_check) {
  if (STATU_GET_NOT(rtc_ctrl_task_obj->ext_rtc_status, EXT_RTC_WRITABLE) && !force_check) {
    return RT_EOK;
  } else {
    // 锁定资源。
    if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
      uint8_t get_mutex = 0;
      while (rt_mutex_take(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock), 50 * (2 ^ get_mutex)) != RT_EOK) {
        if (get_mutex >= 3) {
          return RT_EBUSY;
        }
        get_mutex++;
      }
    }// 只有初始化了保护锁才会检查

    // 读取写保护状态
    memset(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf, 0, sizeof(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf));
    rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x0F;
    rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 1;
    rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 1;
    I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
    ULOG_DEBUG("get sd8804 reg: 0x%02X value: 0x%02X\n", rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1], rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2]);
    rt_thread_mdelay(2);
    if (STATU_GET_NOT(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2], 0x80)) {// 判断写保护已关闭
      // 第一步向FC寄存器写0.该寄存器最高位为只读的写保护位，最低两位永远为0，所以写入时候不用管它。
      rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0xFC;
      rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2] = 0x00;
      rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 2;
      rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 0;
      I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
      rt_thread_mdelay(2);
      // 第二步向FC寄存器的2-6位写入10101
      rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2] = 0x54;
      rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 2;
      rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 0;
      I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
      rt_thread_mdelay(2);
      // 第三步向FC寄存器的2-6位写入01010
      rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2] = 0x28;
      rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 2;
      rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 0;
      I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
      rt_thread_mdelay(2);
      // 第四步向FC寄存器的2-6位写入10111
      rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2] = 0x5C;
      rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 2;
      rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 0;
      I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
      rt_thread_mdelay(2);
    }// 判断写保护已关闭

    // 读写保护状态
    memset(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf, 0, 3);
    rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x0F;
    rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 1;
    rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 1;
    I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
    ULOG_DEBUG("get sd8804 reg: 0x%02X value: 0x%02X\n", rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1], rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2]);
    rt_thread_mdelay(2);

    if (STATU_GET(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2], 0x80)) {// 关闭成功
      STATU_RESET(rtc_ctrl_task_obj->ext_rtc_status, EXT_RTC_WRITABLE);
    } else {// 关闭失败
      STATU_SET(rtc_ctrl_task_obj->ext_rtc_status, EXT_RTC_WRITABLE);
    }

    if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {
      rt_mutex_release(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock));
    }
    return RT_EOK;
  }// 读写保护
}

// 设置RTC时间
rt_err_t Sd8804_Write_Data_Time(Rtc_Ctrl_Task_Def *rtc_ctrl_task_obj, Time_Info_Def *time_obj) {
  // 锁定资源。
  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    uint8_t get_mutex = 0;
    while (rt_mutex_take(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock), 50 * (2 ^ get_mutex)) != RT_EOK) {
      if (get_mutex >= 3) {
        return LL_ERR_BUSY;
      }
      get_mutex++;
    }
  }

  memset(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf, 0, sizeof(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf));
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x00;
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2] = DEC2BCD(time_obj->time_info.tm_sec);
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[3] = DEC2BCD(time_obj->time_info.tm_min);
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[4] = DEC2BCD(time_obj->time_info.tm_hour);
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[5] = 0x01;
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[6] = DEC2BCD(time_obj->time_info.tm_mday);
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[7] = DEC2BCD(time_obj->time_info.tm_mon);
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[8] = DEC2BCD((time_obj->time_info.tm_year) % 100);
  rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 8;
  rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 0;
  I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
  ULOG_DEBUG("get sd8804 reg: 0x%02X value: 0x%02X\n", rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1], rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2]);
  rt_thread_mdelay(2);

  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    rt_mutex_release(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock));
  }
  return RT_EOK;
}// 设置RTC时间

// 读取RTC时间
rt_err_t Sd8804_Read_Data_Time(Rtc_Ctrl_Task_Def *rtc_ctrl_task_obj, Time_Info_Def *time_obj) {
  // 锁定资源。
  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    uint8_t get_mutex = 0;
    while (rt_mutex_take(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock), 50 * (2 ^ get_mutex)) != RT_EOK) {
      if (get_mutex >= 3) {
        return RT_EBUSY;
      }
      get_mutex++;
    }
  }

  memset(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf, 0, sizeof(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf));
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x00;
  rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 1;
  rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 7;
  I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);

  rt_err_t res = RT_EOK;

  if (BCD2DEC(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[8]) >= 100) {
    res = RT_ERROR;
  } else if (rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[7] == 0 || BCD2DEC(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[7]) > 12) {
    res = RT_ERROR;
  } else if (rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[6] == 0 || BCD2DEC(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[6]) > 31) {
    res = RT_ERROR;
  } else if (BCD2DEC(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[4]) > 23) {
    res = RT_ERROR;
  } else if (BCD2DEC(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[3]) > 59) {
    res = RT_ERROR;
  } else if (BCD2DEC(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2]) > 59) {
    res = RT_ERROR;
  }

  if (res == RT_EOK) {
    STATU_RESET(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[4], 0x80);

    time_obj->time_info.tm_year = 2000 + BCD2DEC(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[8]);
    time_obj->time_info.tm_mon = BCD2DEC(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[7]);
    time_obj->time_info.tm_mday = BCD2DEC(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[6]);
    time_obj->time_info.tm_hour = BCD2DEC(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[4]);
    time_obj->time_info.tm_min = BCD2DEC(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[3]);
    time_obj->time_info.tm_sec = BCD2DEC(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2]);
  }

  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    rt_mutex_release(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock));
  }
  return res;
}// 读取RTC时间

// 开启32.768K频率输出
rt_err_t Sd8804_Start_32K(Rtc_Ctrl_Task_Def *rtc_ctrl_task_obj, bool force_check) {
  rt_err_t res = RT_EOK;

  // 如果已经打开就不做操作
  if (STATU_GET(rtc_ctrl_task_obj->ext_rtc_status, EXT_RTC_32K_NVIC) && !force_check) {
    return res;
  }

  // 关闭写保护
  Sd8804_Write_Enable(rtc_ctrl_task_obj, false);

  // 锁定资源。
  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    uint8_t get_mutex = 0;
    while (rt_mutex_take(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock), 50 * (2 ^ get_mutex)) != RT_EOK) {
      if (get_mutex >= 3) {
        return RT_EBUSY;
      }
      get_mutex++;
    }
  }

  memset(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf, 0, sizeof(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf));
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x11;
  rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 1;
  rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 1;
  I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
  rt_thread_mdelay(2);

  //memset(i2c1_ctrl.data_buf, 0, sizeof(i2c1_ctrl.data_buf));
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x11;
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2] = STATU_SET(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2], 0x40);
  rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 2;
  rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 0;
  I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
  rt_thread_mdelay(2);

  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    rt_mutex_release(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock));
  }

  Sd8804_Write_Disable(rtc_ctrl_task_obj, false);
  STATU_SET(rtc_ctrl_task_obj->ext_rtc_status, EXT_RTC_32K_NVIC);
  return res;
}// 开启32.768K频率输出

// 关闭32.768K频率输出
rt_err_t Sd8804_Stop_32K(Rtc_Ctrl_Task_Def *rtc_ctrl_task_obj, bool force_check) {
  rt_err_t res = RT_EOK;

  // 如果已经打开就不做操作
  if (STATU_GET_NOT(rtc_ctrl_task_obj->ext_rtc_status, EXT_RTC_32K_NVIC) && !force_check) {
    return res;
  }
  // 锁定资源。
  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    uint8_t get_mutex = 0;
    while (rt_mutex_take(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock), 50 * (2 ^ get_mutex)) != RT_EOK) {
      if (get_mutex >= 3) {
        return LL_ERR_BUSY;
      }
      get_mutex++;
    }
  }

  memset(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf, 0, sizeof(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf));
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x11;
  rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 1;
  rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 1;
  I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
  rt_thread_mdelay(2);

  //memset(i2c1_ctrl.data_buf, 0, sizeof(i2c1_ctrl.data_buf));
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x11;
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2] = STATU_RESET(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2], 0x40);
  rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 2;
  rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 0;
  I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
  rt_thread_mdelay(2);

  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    rt_mutex_release(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock));
  }

  Sd8804_Write_Disable(rtc_ctrl_task_obj, false);
  STATU_RESET(rtc_ctrl_task_obj->ext_rtc_status, EXT_RTC_32K_NVIC);
  return res;
}// 关闭32.768K频率输出

// 初始化秒中断
rt_err_t Sd8804_Init_Sec_Intc(Rtc_Ctrl_Task_Def *rtc_ctrl_task_obj, bool force_check) {
  rt_err_t res = RT_EOK;

  // 如果已经打开就不做操作
  if (STATU_GET(rtc_ctrl_task_obj->ext_rtc_status, EXT_RTC_INIT_PRE_TIMER_NVIC) && !force_check) {
    return res;
  }

  // 锁定资源。
  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    uint8_t get_mutex = 0;
    while (rt_mutex_take(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock), 50 * (2 ^ get_mutex)) != RT_EOK) {
      if (get_mutex >= 3) {
        return LL_ERR_BUSY;
      }
      get_mutex++;
    }
  }

  // 设置中断类型为频率中断
  memset(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf, 0, sizeof(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf));
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x0F;
  rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 1;
  rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 1;
  I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
  rt_thread_mdelay(2);

  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x0F;
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2] = STATU_SET(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2], 0x20);
  rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 2;
  rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 0;
  I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
  rt_thread_mdelay(2);

  // 设置频率中断的频率为1秒
  memset(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf, 0, sizeof(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf));
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x0D;
  rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 1;
  rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 1;
  I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
  rt_thread_mdelay(2);

  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x0D;
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2] = STATU_RESET(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2], 0x20);
  rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 2;
  rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 0;
  I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
  rt_thread_mdelay(2);

  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    rt_mutex_release(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock));
  }

  STATU_SET(rtc_ctrl_task_obj->ext_rtc_status, EXT_RTC_INIT_PRE_TIMER_NVIC);
  return res;
}// 初始化秒中断

// 关闭秒中断
rt_err_t Sd8804_Stop_Sec_Intc(Rtc_Ctrl_Task_Def *rtc_ctrl_task_obj, bool force_check) {
  rt_err_t res = RT_EOK;

  // 如果已经打开就不做操作
  if (STATU_GET_NOT(rtc_ctrl_task_obj->ext_rtc_status, EXT_RTC_INIT_PRE_TIMER_NVIC) && !force_check) {
    return res;
  }

  // 锁定资源。
  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    uint8_t get_mutex = 0;
    while (rt_mutex_take(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock), 50 * (2 ^ get_mutex)) != RT_EOK) {
      if (get_mutex >= 3) {
        return LL_ERR_BUSY;
      }
      get_mutex++;
    }
  }

  // 打开中断输出
  memset(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf, 0, sizeof(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf));
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x0F;
  rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 1;
  rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 1;
  I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
  rt_thread_mdelay(2);

  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x0F;
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2] = STATU_RESET(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2], 0x02);
  rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 2;
  rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 0;
  I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
  rt_thread_mdelay(2);

  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    rt_mutex_release(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock));
  }
  STATU_RESET(rtc_ctrl_task_obj->ext_rtc_status, EXT_RTC_INIT_PRE_TIMER_NVIC);
  return res;
}// 关闭秒中断

// 设置PPS滤波
rt_err_t Sd8804_Set_PPS_Error_Intc(Rtc_Ctrl_Task_Def *rtc_ctrl_task_obj) {
  rt_err_t res = RT_EOK;
	
	// 这个只是开机设置一次，每次重启都要重新设置，所以没有标志位(标志位目前不够了)

  // 锁定资源。
  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    uint8_t get_mutex = 0;
    while (rt_mutex_take(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock), 50 * (2 ^ get_mutex)) != RT_EOK) {
      if (get_mutex >= 3) {
        return LL_ERR_BUSY;
      }
      get_mutex++;
    }
  }

  // 设置PPS滤波位125ms
  memset(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf, 0, sizeof(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf));
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x17;
  rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 1;
  rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 1;
  I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
  rt_thread_mdelay(2);

  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x17;
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2] = STATU_RESET(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2], 0x03);
  rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 2;
  rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 0;
  I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
  rt_thread_mdelay(2);

  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    rt_mutex_release(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock));
  }

  return res;
}// 设置PPS滤波

// RTC是否发生过关闭， 仅能判断每次上电状态。当RTC发生过重启会置位，任何写操作都会清零这个
rt_err_t Ext_Rtc_Stoped(Rtc_Ctrl_Task_Def *rtc_ctrl_task_obj) {
  rt_err_t res = RT_EOK;
  // 锁定资源。
  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    uint8_t get_mutex = 0;
    while (rt_mutex_take(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock), 50 * (2 ^ get_mutex)) != RT_EOK) {
      if (get_mutex >= 3) {
        return RT_EBUSY;
      }
      get_mutex++;
    }
  }

  // 读取重启标志位
  memset(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf, 0, sizeof(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf));
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x0E;
  rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 1;
  rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 1;
  I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);
  if (STATU_GET(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[2], 0x02)) {
    STATU_SET(rtc_ctrl_task_obj->ext_rtc_status, EXT_RTC_STOPED);
  } else {
    STATU_RESET(rtc_ctrl_task_obj->ext_rtc_status, EXT_RTC_STOPED);
  }


  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    rt_mutex_release(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock));
  }
  return res;
}
// RTC是否发生过关闭

// RTC复位校准寄存器。确保每次都正确复位
rt_err_t Ext_Rtc_Reset_Tmp_Ttf(Rtc_Ctrl_Task_Def *rtc_ctrl_task_obj) {
  rt_err_t res = RT_EOK;
  // 锁定资源。
  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    uint8_t get_mutex = 0;
    while (rt_mutex_take(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock), 50 * (2 ^ get_mutex)) != RT_EOK) {
      if (get_mutex >= 3) {
        return RT_EBUSY;
      }
      get_mutex++;
    }
  }

  // 清空校准寄存器
  memset(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf, 0, sizeof(rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf));
  rtc_ctrl_task_obj->i2c_ctrl_obj.data_buf[1] = 0x52;
  rtc_ctrl_task_obj->i2c_ctrl_obj.send_data_len = 2;
  rtc_ctrl_task_obj->i2c_ctrl_obj.receive_data_len = 0;
  I2C_Master_Translate_IT(rtc_ctrl_task_obj->i2c_ctrl_obj.i2c_cm, &(rtc_ctrl_task_obj->i2c_ctrl_obj), rtc_addr);

  if (strlen(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock.parent.parent.name) != 0) {// 只有初始化了保护锁才会检查
    rt_mutex_release(&(rtc_ctrl_task_obj->i2c_ctrl_obj.port_lock));
  }
  return res;
}
// RTC复位校准寄存器。确保每次都正确复位

rt_err_t Init_Rtc_Task_Obj(Rtc_Ctrl_Task_Def *obj, CM_I2C_TypeDef *CM_I2Cx, char *i2c_name) {
  char str_buf[16];
  if (RT_NULL == i2c_name || RT_NULL == CM_I2Cx) {
    return RT_EEMPTY;
  }

  if (Init_I2c_Data_Task_Obj(&(obj->i2c_ctrl_obj), i2c_name, CM_I2Cx) != RT_EOK) {
    return RT_ERROR;
  }

  obj->rtc_status = 0;
  obj->ext_rtc_status = 0;
  obj->time_update_squence_type = RTC_FLASH_PRE_HOUR;

  memset(str_buf, 0, 16);
  rt_sprintf(str_buf, "%s_rtc_event", i2c_name);
  if (rt_event_init(&(obj->rtc_ctrl_event), str_buf, RT_IPC_FLAG_PRIO) != RT_EOK) {
    return RT_ERROR;
  }

  return RT_EOK;
}


// 整个RTC任务分为两个部分。一个是预初始化结构体。一个是启动任务
void Init_Time_Pre_Obj(void) {
  if (Init_Time_Info_Obj(&sys_time_info, "sys time obj") != RT_EOK) {
    ULOG_ERROR("Init Time Info error!\n");
    for (;;) { ; }
  }
  if (Init_Rtc_Task_Obj(&rtc_ctrl_task, CM_I2C2, "rtc ctrl obj") != RT_EOK) {
    ULOG_ERROR("Init Rtc Obj error!\n");
    for (;;) { ; }
  }
  STATU_SET(rtc_ctrl_task.rtc_status, HAD_EXT_RTC);
  STATU_SET(rtc_ctrl_task.rtc_status, HAD_INNER_RTC);
  // 刚开始没时间所以粗略从RTC同步一下
  sys_time_info.Update_Time_From_Innner_Rtc_Delay(&sys_time_info);
  sys_time_info.Build_Rtc_Time_Str(&sys_time_info);


  // rtc刷新任务
  rtc_flash_tid = rt_thread_create("Rtc Flash Task",
                                   Flash_Time_Info_Thread, RT_NULL,
                                   THREAD_STACK_SIZE,
                                   6, THREAD_TIMESLICE);
  // 这里优先级比别的任务要高一点，为了保证时间准确

  // rtc_tid任务
  rtc_check_tid = rt_thread_create("Rtc Check Task",
                                   Time_Manager_Thread, RT_NULL,
                                   THREAD_STACK_SIZE,
                                   7, THREAD_TIMESLICE);
  // 这里优先级比别的任务要高一点，为了保证时间准确

  if (rtc_check_tid != RT_NULL &&
      (strlen(sys_time_info.rtc_lock.parent.parent.name) > 0 && strlen(rtc_ctrl_task.i2c_ctrl_obj.name) > 0)) {
    rt_thread_startup(rtc_check_tid);
  }
}// 预初始化结构体
